<?php 
namespace app\api\controller\v1;

use think\Db;
//登录后基类
class TickerController extends BaseController{

	
	function  market(){
		$rows = Db::name('ticker')->where('status',1)->order('coin asc')->select()->toArray();
		$rows = $this->deal_ticker($rows);
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rows]);

	}



	function ustdtocoin(){
		$amount = input('param.amount/s');
		if($amount<=0){
			return json(['error'=>1,'msg'=>$this->lang['AMOUNT_ERROR']]);
		}
		if(empty($coin)){
			return json(['error'=>1,'msg'=>$this->lang['AMOUNT_ERROR']]);
		}
		$rows = Db::name('ticker')->where('status',1)->order('coin asc')->select()->toArray();
		$map = $this->getmap($rows);
	}


	function  cointoustd(){
		$rows = Db::name('ticker')->where('status',1)->order('coin asc')->select()->toArray();
		$map = $this->getmap($rows);
		$map['ustd'] =1;
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$map]);
	}


	protected  function getmap($rows,$exchange='huobipro'){
		$map  =[];
		foreach ($rows as $key => $value) {
			if(!array_key_exists($value['coin'],$map)){
				$map[$value['coin']] = $value['price'];
			}
		}
		return $map;
	}



	protected function deal_ticker($rows){
		$rets =[];
		$exchanges=[];
		foreach ($rows as $key => $value) {
			$row['market']    = $value['market'];
			$row['coin']      = $value['coin'];
			$row['price']     = $value['price'];
			$row['update_at'] = date('Y-m-d',$value['update_time']);
			$rets[strtolower($value['exchange_name'])][]= $row;
		}
		return $rets;
	}

}